Create new file button with sys save file dialog box

James Peret 8 years ago
parent
commit
fc4d7aceb0

+ 6 - 1
app/index.html

@@ -23,6 +23,11 @@
23 23
     <script src="scripts/controllers/note-edit-ctrl.js"></script>
24 24
     <script src="scripts/services/file-service.js"></script>
25 25
     <script src="scripts/services/date-formatter.js" charset="utf-8"></script>
26
+
27
+    <script>
28
+     var remote = require('remote'); 
29
+     var dialog = remote.require('dialog');
30
+    </script>
26 31
   </head>
27 32
 
28 33
   <body ng-app="codexApp">
@@ -31,7 +36,7 @@
31 36
       <header class="toolbar toolbar-header" ng-controller="HeaderCtrl">
32 37
         <h1 class="title">Codex</h1>
33 38
         <div class="toolbar-actions">
34
-          <button class="btn btn-default">
39
+          <button class="btn btn-default" ng-click="createNewNote()">
35 40
             <span class="icon icon-plus"></span>
36 41
           </button>
37 42
           <div class="btn-group pull-right">

+ 4 - 10
app/scripts/codex-app.js

@@ -37,26 +37,20 @@ angular
37 37
       controller: 'AppCtrl'
38 38
     })
39 39
     .state('note-view', {
40
-      url: "/notes",
40
+      url: "/note-view",
41 41
       templateUrl: "views/note-view.html",
42 42
       controller: 'NoteViewCtrl',
43 43
       resolve: {
44 44
         pageData: function($stateParams) {
45
-          console.log('resolve ok')
45
+          //console.log('resolve ok')
46 46
           return 'resolve ok';
47 47
         },
48 48
       }
49 49
     })
50 50
     .state('note-edit', {
51
-      url: "/notes",
51
+      url: "/note-edit",
52 52
       templateUrl: "views/note-edit.html",
53
-      controller: 'NoteEditCtrl',
54
-      resolve: {
55
-        pageData: function($stateParams) {
56
-          console.log('resolve ok')
57
-          return 'resolve ok';
58
-        },
59
-      }
53
+      controller: 'NoteEditCtrl'
60 54
     })
61 55
     $urlRouterProvider.otherwise("/");
62 56
 

+ 20 - 6
app/scripts/controllers/header-ctrl.js

@@ -8,15 +8,32 @@
8 8
  * Controller of the domainManagerApp
9 9
  */
10 10
 angular.module('codexApp.header', [])
11
-  .controller('HeaderCtrl',['$scope', '$rootScope', '$state', function ($scope,  $rootScope, $state) {
11
+  .controller('HeaderCtrl',['$scope', '$rootScope', '$state', 'FileService', function ($scope,  $rootScope, $state, FileService) {
12 12
 
13
-    console.log('Header loaded')
13
+    console.log('-> Header loaded')
14 14
 
15 15
     $scope.noteViewBtnClass = "";
16 16
     $scope.noteEditBtnClass = "";
17 17
 
18
+    // Create New Note
19
+
20
+    $scope.createNewNote = function() {
21
+      dialog.showSaveDialog({ defaultPath: FileService.getDefaultNotesDir(), filters: [ { name: 'markdown', extensions: ['md'] }] }, function (fileName) {
22
+        var fs = require('fs');
23
+        if (fileName === undefined) return;
24
+        fs.writeFile(fileName, "", function (err) {
25
+          console.log("-> CREATE NEW NOTE: " + fileName)
26
+          var note = FileService.getNote(fileName);
27
+          FileService.setCurrentNote(note)
28
+          console.log(note)
29
+          $scope.activateNoteEdit();
30
+        });
31
+      });
32
+    }
33
+
34
+    // Note View active button
35
+
18 36
     $scope.activateNoteView = function() {
19
-      $rootScope.$broadcast('activate-note-view');
20 37
       $rootScope.$broadcast('window-view:change');
21 38
       $state.go("note-view");
22 39
       $scope.noteViewBtnClass = "active";
@@ -24,7 +41,6 @@ angular.module('codexApp.header', [])
24 41
     }
25 42
 
26 43
     $scope.activateNoteEdit = function() {
27
-      $rootScope.$broadcast('activate-note-edit');
28 44
       $rootScope.$broadcast('window-view:change');
29 45
       $state.go("note-edit");
30 46
       $scope.noteViewBtnClass = "";
@@ -41,7 +57,6 @@ angular.module('codexApp.header', [])
41 57
         $scope.noteViewBtnClass = "";
42 58
         $scope.noteEditBtnClass = "";
43 59
       }
44
-      //console.log($scope.raw_data);
45 60
     });
46 61
 
47 62
     $rootScope.$on('main-window:note-view', function() {
@@ -54,7 +69,6 @@ angular.module('codexApp.header', [])
54 69
         $scope.noteViewBtnClass = "active";
55 70
         $scope.noteEditBtnClass = "";
56 71
       }
57
-      //console.log($scope.raw_data);
58 72
     });
59 73
 
60 74
   }]);

+ 2 - 44
app/scripts/controllers/note-edit-ctrl.js

@@ -10,65 +10,23 @@
10 10
 angular.module('codexApp.noteEdit', [])
11 11
   .controller('NoteEditCtrl',['$scope', '$rootScope', '$state', 'FileService', function ($scope,  $rootScope, $state, FileService) {
12 12
 
13
-    var marked = require('marked');
14
-    var filesystem = require("fs");
15
-
16
-    console.log('Note opened!')
13
+    console.log('-> Note Editor opened!')
17 14
 
18 15
     $scope.note = FileService.getCurrentNote();
19 16
     $scope.container = "note-container";
20 17
     $scope.raw_data = "";
21
-    $scope.showNoteView = true;
22
-    $scope.showNoteEdit = false;
23 18
     $scope.savedBeforeQuit = false;
24 19
 
25
-
26
-    filesystem.readFile($scope.note.path, function(err, data) {
27
-      var str = String.fromCharCode.apply(null, data)
28
-      if(!$scope.$$phase) {
29
-        $scope.$apply(function(){
30
-          $scope.note.data = str;
31
-          $scope.raw_data = str
32
-        });
33
-      } else {
34
-          $scope.note.data = str;
35
-          $scope.raw_data = str;
36
-      }
37
-      //console.log($scope.raw_data);
38
-      var a = document.getElementsByTagName('a'), ajax;
39
-      for (var i=0; i<a.length; ++i) {
40
-         a[i].addEventListener('click', handleAnchor, false);
41
-      }
42
-      function handleAnchor(e){
43
-          e.preventDefault();
44
-          if(ajax) ajax.abort();
45
-          ajax = new XMLHttpRequest();
46
-          ajax.onload = updateContent;
47
-          ajax.open("get", this.href, true);
48
-          ajax.send();
49
-          console.log("-> Prevented link from opening: " + e.srcElement.href);
50
-      }
51
-      function updateContent() {
52
-          // Do something with `this.responseText`
53
-      }
54
-    });
55
-
56
-    $scope.marked = function(str) {
57
-      return marked(str);
58
-    }
59
-
60 20
     $rootScope.$on('window-view:change', function() {
61 21
       if($scope.savedBeforeQuit == false) {
62 22
         $scope.savedBeforeQuit = true;
63 23
         FileService.saveFile($scope.note.path, $scope.raw_data)
64 24
       }
65
-
66 25
     });
67 26
 
68
-
69 27
     $scope.aceLoaded = function(_editor) {
70 28
        _editor.setReadOnly(false);
71
-      console.log($scope.raw_data);
29
+      //console.log($scope.raw_data);
72 30
     };
73 31
 
74 32
     $scope.aceChanged = function(e) {

+ 7 - 25
app/scripts/controllers/note-view-ctrl.js

@@ -13,7 +13,7 @@ angular.module('codexApp.noteView', [])
13 13
     var marked = require('marked');
14 14
     var filesystem = require("fs");
15 15
 
16
-    console.log('Note opened!')
16
+    console.log('-> Note View opened!')
17 17
 
18 18
     $scope.note = FileService.getCurrentNote();
19 19
     $scope.container = "note-container";
@@ -51,32 +51,14 @@ angular.module('codexApp.noteView', [])
51 51
     });
52 52
 
53 53
     $scope.marked = function(str) {
54
-      return marked(str);
55
-    }
56
-
57
-    $rootScope.$on('activate-note-view', function() {
58
-      if(!$scope.$$phase) {
59
-        $scope.$apply(function(){
60
-          $scope.showNoteView = true;
61
-          $scope.showNoteEdit = false;
62
-        });
54
+      if(str != "" && str != undefined) {
55
+        return marked(str);
63 56
       } else {
64
-        $scope.showNoteView = true;
65
-        $scope.showNoteEdit = false;
57
+        return str;
66 58
       }
67
-    });
68 59
 
69
-    $rootScope.$on('activate-note-edit', function() {
70
-      if(!$scope.$$phase) {
71
-        $scope.$apply(function(){
72
-          $scope.showNoteView = false;
73
-          $scope.showNoteEdit = true;
74
-        });
75
-      } else {
76
-        $scope.showNoteView = false;
77
-        $scope.showNoteEdit = true;
78
-      }
79
-      //console.log($scope.raw_data);
80
-    });
60
+    }
61
+
62
+
81 63
 
82 64
   }]);

+ 1 - 1
app/scripts/controllers/sidebar-ctrl.js

@@ -10,7 +10,7 @@
10 10
 angular.module('codexApp.sidebar', [])
11 11
   .controller('SidebarCtrl',['$scope', '$rootScope', '$state', function ($scope,  $rootScope, $state) {
12 12
 
13
-    console.log('Sidebar loaded')
13
+    console.log('-> Sidebar loaded')
14 14
 
15 15
     $scope.goToAllNotes = function() {
16 16
       $rootScope.$broadcast('main-window:note-list');

+ 25 - 2
app/scripts/services/file-service.js

@@ -2,6 +2,7 @@ angular.module('codexApp')
2 2
 .service('FileService', [ '$rootScope', '$http',  function($rootScope, $http) {
3 3
 
4 4
   var notes_dir = "/Users/james/dev/codex/codex";
5
+  var default_notes_dir = "/Users/james/dev/codex/codex/inbox";
5 6
   var notes = [];
6 7
   var current_note = "";
7 8
 
@@ -49,7 +50,7 @@ angular.module('codexApp')
49 50
         } else {
50 51
           if(file != ".DS_Store") {
51 52
             size = size + stat["size"];
52
-            console.log("* " + stat["size"] + " KB -> " + file_path)
53
+            //console.log("* " + stat["size"] + " KB -> " + file_path)
53 54
           }
54 55
         }
55 56
     });
@@ -170,6 +171,24 @@ angular.module('codexApp')
170 171
     });
171 172
   }
172 173
 
174
+  this.getNote = function(file_path){
175
+    var filesystem = require("fs");
176
+    var stat = filesystem.statSync(file_path);
177
+    var file_obj = {
178
+      title: "",
179
+      path: file_path,
180
+      size: stat['size'],
181
+      type: getFileType(file_path),
182
+      author: "",
183
+      index: "",
184
+      id: "",
185
+      created_at: dateFormat(stat["birthdate"], "mediumDate"),
186
+      modified_at: dateFormat(stat["mtime"], "mediumDate"),
187
+      accessed_at: dateFormat(stat["atime"], "mediumDate")
188
+    }
189
+    return file_obj
190
+  }
191
+
173 192
 
174 193
   // RESPONSE
175 194
   this.getNotes = function() {
@@ -185,7 +204,11 @@ angular.module('codexApp')
185 204
     //console.log("searcing for: " + note_id)
186 205
     current_note = note;
187 206
     //console.log(current_note);
188
-    console.log("Current_note: " + current_note.title)
207
+    //console.log("Current_note: " + current_note.title)
208
+  }
209
+
210
+  this.getDefaultNotesDir = function() {
211
+    return default_notes_dir;
189 212
   }
190 213
 
191 214
 }])